<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %><%--
  Created by IntelliJ IDEA.
  User: wrc
  Date: 2021/4/12
  Time: 14:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>搜索结果</title>
  <link rel="stylesheet" href="book.css" type="text/css">
</head>
<body>
<style>
  .div1{
    text-align: center;
    margin-top: 10px;
  }
</style>

<%--<p align="center">搜索音乐:</p>--%>
<%--<form class="div1" action="index.jsp" method="get">--%>
<%--  <input type="text" name="search">--%>
<%--  <input type="submit" value="搜索">--%>
<%--</form>--%>
<table align="center" border="1" width="50%">


  <%
    request.setCharacterEncoding("utf-8");
    String result = request.getParameter("search");
    DataSource ds = null;

      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");
      //从Context中lookup数据源。
      ds = (DataSource) envCtx.lookup("jdbc/MSSQLDS");

      if (ds != null) {
        out.println("已经获得数据源DataSource!");
        Connection con = ds.getConnection();
        //Statement stmt = con.createStatement();

//    Class.forName("com.mysql.jdbc.Driver");
//    Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/mywood?characterEncoding=utf-8","root","000000");
    String sql = "select * from music where singer=? or musicname=?";
    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setString(1, result);
    pstmt.setString(2, result);
    ResultSet rs = pstmt.executeQuery();
    while(rs.next()){
  %>
  <tr><td><%=rs.getInt("id") %></td>
    <td><%=rs.getString("musicname") %></td>
    <td><%=rs.getString("singer") %></td>
    <td><%=rs.getInt("rank") %></td></tr>

  <%
    }
    rs.close();
    pstmt.close();
    con.close();
    }
  %>
</table>
</body>
</html>
